<!-- 推广员列表 -->
<template>
  <div class="joinIntro sys-content">
    <page-mod>
      <el-card>
        <div class="query-box">
          <el-form :model="query" inline>
            <el-form-item label="标题:">
              <el-input v-model="query.title" placeholder="请输入标题" clearable/>
            </el-form-item>
            <el-form-item label="启用状态:">
              <el-select v-model="query.enableStatus" placeholder="请选择" clearable>
                <el-option label="启用" value="1" />
                <el-option label="禁用" value="0" />
              </el-select>
            </el-form-item>
          </el-form>
          <div class="btns">
            <el-button icon="Search" color="#16b777" @click="load(1)">查询</el-button>
            <el-button icon="Plus" color="#FFB800" @click="toAdd">新增</el-button>
            <el-button icon="Refresh" color="#FF5722" @click="resetSearch">重置</el-button>
          </div>
        </div>
      </el-card>
    </page-mod>
    <page-mod title="加盟介绍数据" icon="Histogram" :showBtn="false" :padBottom="false">
      <el-card v-loading="loading" element-loading-text="拼命加载中">
        <el-table ref="tableRef" :data="dataList" max-height="493" border>
          <el-table-column label="序号" type="index" align="center" width="60"/>
          <el-table-column label="标题" prop="title" align="center"></el-table-column>
          <el-table-column label="主图" prop="pic" align="center">
            <template #default="{row}">
              <div class="img-cell">
                <el-image :src="row.pic" :preview-src-list="[row.pic]" preview-teleported/>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="经销商类型" prop="category" align="center">
            <template #default="{row}">
              <span v-if="row.category === '1'">门店经销商</span>
              <span v-if="row.category === '2'">区/县运营中心</span>
              <span v-if="row.category === '3'">市运营中心</span>
              <span v-if="row.category === '4'">省运营中心</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="enableStatus" align="center">
            <template #default="{row}">
              <el-tag :type="row.enableStatus ? 'success' : 'danger'">{{ row.enableStatus ? '已启用' : '已禁用' }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createdTime" align="center">
            <template #default="{row}">
              <span>{{ $date(row.createdTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="140">
            <template #default="{row}">
              <el-button type="primary" icon="Edit" link @click="toUpdate(row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination 
          class="paging"
          v-show="query.total"
          small
          layout="total, sizes, prev, pager, next, jumper" 
          :current-page="query.current" 
          :page-size="query.size"
          :total="query.total"
          @current-change="load"
          @size-change="sizeChange">
        </el-pagination>
      </el-card>
    </page-mod>

    <!-- 编辑弹窗 -->
    <el-dialog v-model="show" :title="form.introduceId ? '编辑' : '新增'" top="10vh" width="55%" :destroy-on-close="true" center draggable @close="resetForm">
      <el-form ref="formRef" :model="form" :rules="rules" size="large" label-position="right" label-width="100px">
        <el-form-item label="标题:" prop="title" required>
          <el-input v-model="form.title" placeholder="请输入标题" clearable/>
        </el-form-item>
        <el-form-item label="经销商类型:" prop="category" required>
          <el-select v-model="form.category" placeholder="请选择经销商类型" clearable>
            <el-option label="门店经销商" value="1" />
            <el-option label="区/县运营中心" value="2" />
            <el-option label="市运营中心" value="3" />
            <el-option label="省运营中心" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否启用:" prop="enableStatus">
          <el-switch 
            v-model="form.enableStatus"
            :active-value="1"
            :inactive-value="0"
            style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"/>
        </el-form-item>
        <el-form-item label="主图:" prop="pic" required>
          <img-cutter v-model="form.pic" :isCard="true" :multiple="false" width="180" height="120"></img-cutter>
        </el-form-item>
        <el-form-item label="加盟详情:" prop="details" required>
          <tinymce-editor v-model="form.details" ref="editorRef"></tinymce-editor>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" size="default" @click="doSubmit">保存</el-button>
        <el-button size="default" @click="show = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="joinIntro">
  import { saveOrUpdateJoinIntroApi, getJoinIntroListPagesApi } from '@/api/api'
  import { ref, reactive, toRefs, getCurrentInstance, onActivated } from 'vue'

  // 获取注册的全局实列
  const currentThis = getCurrentInstance().appContext.config.globalProperties
  const formRef = ref(null)
  const editorRef = ref(null)

  const currentData = reactive({
    loading: false,
    show: false,
    query: {
      title: '',
      enableStatus: '',
      current: 1,
      size: 20,
      total: 0
    },
    form: {
      introduceId: '',
      title: '',
      category: '',
      enableStatus: 1,
      pic: '',
      details: ''
    },
    rules: {
      title: [{ required: true, message: '请输入标题', trigger: ['blur', 'change'] }],
      category: [{ required: true, message: '请选择经销商类型', trigger: ['blur', 'change'] }],
      pic: [{ required: true, message: '请上传主图', trigger: ['blur', 'change'] }],
      details: [{ required: true, message: '请输入加盟详情', trigger: ['blur', 'change'] }],
    },
    dataList: []
  })
  let {loading, show, query, form, rules, dataList} = toRefs(currentData)

  // 查询
  function load(page) {
    currentData.loading = true
    currentData.query.current = page
    getJoinIntroListPagesApi(currentData.query).then(res => {
      currentData.loading = false
      if(res.code === '200') {
        currentData.dataList = res.data.pages
        currentData.query.total = res.data.total
      } else {
        currentThis.$message.error(res.message)
      }
    })
  }

  // 新增或编辑
  function doSubmit() {
    formRef.value.validate((valid) => {
      if (valid) {
        currentThis.$messageBox.confirm(`确认${currentData.form.introduceId ? '修改' : '新增'}吗？`).then(() => {
          let loading = currentThis.$loading(`正在${currentData.form.introduceId ? '修改' : '新增'}`)
          saveOrUpdateJoinIntroApi(`${currentData.form.introduceId ? 'update' : 'save'}`, currentData.form).then(res => {
            loading.close()
            if(res.code === '200') {
              currentThis.$message.success(res.message)
              currentData.show = false
              load(currentData.form.introduceId ? currentData.query.current : 1)
            } else {
              currentThis.$message.error(res.message)
            }
          })
        }).catch(() => {})
      } else {
        return false;
      }
    })
  }

  function toAdd() {
    currentData.form.details = ''
    currentData.show  = true
  }

  // 修改前弹窗
  function toUpdate(e) {
    currentData.form = {...e}
    currentData.show  = true
  }
  
  // 重置查询
  function resetSearch() {
    currentData.query.title = ''
    currentData.query.enableStatus = ''
    load(1)
  }

  // 编辑弹窗关闭重置数据
  function resetForm() {
    currentData.form = {
      introduceId: '',
      title: '',
      category: '',
      enableStatus: 1,
      pic: '',
      details: ''
    }
    formRef.value.resetFields()
    editorRef.value.destroyEditor()
  }

  // 分页每页显示条数改变执行
  function sizeChange(size) {
    currentData.query.size = size
    load(1)
  }

  onActivated(() => {
    load(currentData.query.current)
  })
</script>

<style lang="less" scoped>
.joinIntro{
  :deep(.el-table){
    .img-cell{
      display: flex;
      justify-content: center;
      align-items: center;
      .el-image{
        width: 80px; 
        height: 50px;
        border-radius: 5px;
      }
    }
  }
  :deep(.el-dialog){
    .el-dialog__body{
      max-height: 600px;
      overflow: hidden auto;
    }
    .el-input, .el-textarea{
      width: 300px;
    }
    .el-select{
      .el-input{
        width: 250px !important;
      }
    }
  }
}
</style>